﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Web_PurchaseOrde : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["current"] = "Add Purchase Order";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }
        if (!Page.IsPostBack)//refresh
        {
            SetInitialRow();
            ViewState["index"] = 0;
            ViewState["total"] = 0;
            setprice(0);
        }



    }

    private void SetInitialRow()
    {
        try
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            //dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("DropDownList3", typeof(string)));
            dt.Columns.Add(new DataColumn("Column2", typeof(string)));
            dt.Columns.Add(new DataColumn("Column3", typeof(string)));
            dt.Columns.Add(new DataColumn("Column4", typeof(string)));
            dt.Columns.Add(new DataColumn("Column5", typeof(string)));
            dr = dt.NewRow();
            //dr["RowNumber"] = 1;
            dr["DropDownList3"] = string.Empty;
            dr["Column2"] = string.Empty;
            dr["Column3"] = string.Empty;
            dr["Column4"] = string.Empty;
            dr["Column5"] = string.Empty;
            dt.Rows.Add(dr);
            //dr = dt.NewRow();

            //Store the DataTable in ViewState
            ViewState["CurrentTable"] = dt;

            Gridview1.DataSource = dt;
            Gridview1.DataBind();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//initial table

    private void AddNewRowToGrid()//add new row to table
    {
        try
        {
            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
                        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                        TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                        TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");

                        drCurrentRow = dtCurrentTable.NewRow();
                        //drCurrentRow["RowNumber"] = i + 1;


                        dtCurrentTable.Rows[i - 1]["DropDownList3"] = box1.Text;
                        dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                        dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                        dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
                        dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;

                        rowIndex++;

                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    ViewState["index"] = dtCurrentTable.Rows.Count - 1;
                    Gridview1.DataSource = dtCurrentTable;
                    Gridview1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            SetPreviousData();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }

    private void SetPreviousData()
    {
        try
        {
            int rowIndex = 0;
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
                        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                        TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                        TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");


                        box1.Text = dt.Rows[i]["DropDownList3"].ToString();
                        box2.Text = dt.Rows[i]["Column2"].ToString();
                        box3.Text = dt.Rows[i]["Column3"].ToString();
                        box4.Text = dt.Rows[i]["Column4"].ToString();
                        box5.Text = dt.Rows[i]["Column5"].ToString();

                        rowIndex++;
                    }
                }
            }
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//set previous data

    protected void DropDownList1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            string sup = DropDownList1.Text;
            string result = Bestlife.ERP.BusinessRules.productBV_PV.contactperson(sup);
            DropDownList2.Text = result;
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//load supplier contact person

    protected void ASPxGridView1_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
    {
        try
        {
            if (e.Column.FieldName == "Total")
            {
                decimal price = Convert.ToDecimal(e.GetListSourceFieldValue("Pur_Price"));
                int qty = Convert.ToInt32(e.GetListSourceFieldValue("Qty"));
                decimal tot = price * qty;
                e.Value = tot;
            }
            if (e.Column.FieldName == "Pur_Price")
            {
                string pro_name = Convert.ToString(e.GetListSourceFieldValue("Pro_Name"));
                decimal price = Bestlife.ERP.BusinessRules.productBV_PV.productprice(pro_name);
                e.Value = price;
            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//calculate total and unit price

    protected void ButtonAdd_Click1(object sender, EventArgs e)
    {
        try
        {
            int rowIndex = 0;
            DropDownList box1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("DropDownList3");
            TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
            TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
            TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
            TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox5");

            if (box3.Text != null)
            {
                if (box4.Text != "")
                {
                    if (box5.Text != "")
                    {
                        setprice(0);
                        AddNewRowToGrid();

                    }
                    else
                    {
                        ASPxLabel3.Visible = true;
                        ASPxLabel3.Text = "You must enter a value for all required fields ";
                        setprice(0);

                    }

                }
                else
                {
                    ASPxLabel3.Visible = true;
                    ASPxLabel3.Text = "You must enter a value for all required fields ";
                    setprice(0);

                }
            }
            else
            {
                ASPxLabel3.Visible = true;
                ASPxLabel3.Text = "You must enter a value for all required fields ";
                setprice(0);

            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }//row adding

    protected void ButtonAdd_Click2(object sender, EventArgs e)
    {
        try
        {
            Gridview1.EditIndex = -1;
            Gridview1.DataBind();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }


    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            // int index =Gridview1.SelectedIndex+1;  
            int index = int.Parse(ViewState["index"].ToString());
            setprice(index);
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }

    }

    public void setprice(int index)
    {
        try
        {
            Bestlife.ERP.Entities.E_PurchaseOrder porder = new Bestlife.ERP.Entities.E_PurchaseOrder();
            DropDownList stock = (DropDownList)Gridview1.Rows[index].FindControl("DropDownList4");
            string stockid = stock.Text;
            Bestlife.ERP.BusinessRules.B_PurchaseOrder.getprice(stockid);
            TextBox box = (TextBox)Gridview1.Rows[index].Cells[3].FindControl("TextBox6");
            TextBox box3 = (TextBox)Gridview1.Rows[index].Cells[3].FindControl("TextBox3");
            box3.Text = porder.Unitvalue.ToString();
            box.Text = porder.Retprice.ToString();
        }
        catch
        {
            Response.Redirect("404.ASPX");
        }

    }

    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

        try
        {
            int index = int.Parse(ViewState["index"].ToString());
            TextBox box3 = (TextBox)Gridview1.Rows[index].Cells[3].FindControl("TextBox3");
            TextBox box4 = (TextBox)Gridview1.Rows[index].Cells[4].FindControl("TextBox4");
            decimal qty;
            decimal price;
            try
            {
                qty = decimal.Parse(box4.Text);
                price = decimal.Parse(box3.Text);
                ASPxLabel3.Visible = false;
                decimal tot = Bestlife.ERP.BusinessRules.B_PurchaseOrder.linetot(qty, price);
                ViewState["total"] = decimal.Parse(ViewState["total"].ToString()) + tot;
                ASPxTextBox2.Text = ViewState["total"].ToString();
                TextBox box5 = (TextBox)Gridview1.Rows[index].Cells[5].FindControl("TextBox5");
                box5.Text = tot.ToString();
            }

            catch
            {
                qty = 0;
                price = 0;
                ASPxLabel3.Visible = true;
                ASPxLabel3.Text = "Invalid data type";

            }

        }
        catch
        {
            Response.Redirect("404.ASPX");
        }
    }

    #region insert
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DropDownList box1 = (DropDownList)Gridview1.Rows[i].Cells[1].FindControl("DropDownList3");
                    TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[i].Cells[4].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[i].Cells[5].FindControl("TextBox5");

                    string pro = box1.Text;
                    string description = box2.Text;
                    string unitvalue = box3.Text;
                    string qty = box4.Text;

                    Bestlife.ERP.Entities.E_PurchaseOrder order = new Bestlife.ERP.Entities.E_PurchaseOrder();
                    order.Or_ID1 = ASPxTextBox1.Text;
                    order.Proname = pro;
                    order.Qty = decimal.Parse(qty);
                    order.Unitvalue = decimal.Parse(unitvalue);
                    order.Description = description;



                    Bestlife.ERP.BusinessRules.B_PurchaseOrder.insert(order, user);


                }

                Bestlife.ERP.Entities.E_PurchaseOrder order1 = new Bestlife.ERP.Entities.E_PurchaseOrder();

                order1.Or_ID1 = ASPxTextBox1.Text;
                order1.Contrat = DropDownList2.Text;
               
                order1.Issu = ASPxDateEdit1.Date;
                order1.Supplier = DropDownList2.Text;

                Bestlife.ERP.BusinessRules.B_PurchaseOrder.insertorder(order1,user);
                Bestlife.ERP.BusinessRules.B_PurchaseOrder.insertsupp(order1, user);

                Bestlife.ERP.Entities.E_PurchaseOrder total = new Bestlife.ERP.Entities.E_PurchaseOrder();
                total.Total = decimal.Parse(ASPxTextBox2.Text);
                total.Or_ID1 = ASPxTextBox1.Text;
                Boolean result = Bestlife.ERP.BusinessRules.B_PurchaseOrder.total(total, user);
                if (result)
                {
                    ASPxLabel2.Text = "You have succesfully added your purchase order";
                    ASPxLabel2.Visible = true;
                    ASPxImage1.Visible = true;
                }

            }

        }
        catch
        {
            ASPxLabel2.Text = "Purches order fail";
            ASPxLabel2.Visible = true;
        }
    }
    #endregion

    protected void ASPxButton2_Click(object sender, EventArgs e)
    {

        Response.Redirect("PurchaseOrde.aspx");


    }//cancle
    protected void ASPxDateEdit2_DateChanged(object sender, EventArgs e)
    {

    }
}
